<template>
    <bi-table :loading="loading" :data="list" min-height="300">
        <bi-table-column label="Name" name="name" width="100"></bi-table-column>
        <bi-table-column label="驱动" name="driver" width="100"></bi-table-column>
        <bi-table-column label="内容" name="title"></bi-table-column>
        <bi-table-column label="管理" width="120"  v-slot="{row}">
            <bi-button type="primary" @click="handleEdit(row)" :disabled="row['default']">编辑</bi-button>
            <bi-button type="warning" @click="handleDel(row)" :disabled="row['default'] && app==''">删除</bi-button>
        </bi-table-column>
    </bi-table>
</template>

<script>
import editDialog from './editDialog.vue';
export default{
    props:{
        type:{
            type:String,
            default:''
        },
        app:{
            type:String,
            default:''
        }
    },
    data(){
        return {
            param:undefined,
            list:[],
            loading:false
        }
    },
    methods:{
        getList(){
            
            if (this.type == '') return;
            this.loading = true;
            this.$ajax({
                url:this.$url({app:'admin',controller:'driver',action:'list'}),
                data:{
                    type:this.type,
                    app:this.app
                },
                success:(data)=>{
                    this.loading = false;
                    this.list = data.list;
                    this.param = data.param;
                }
            });
        },
        handleEdit(row){
            this.$dialog({
                title:'编辑驱动',
                type:'vue',
                content:admin.createApp(editDialog,{type:this.type,app:this.app,name:row.name,item:row.config,param:this.param}),
                callback:()=>{
                    this.getList();
                }
            });
        },
        handleDel(row){
            this.$confirm('确定要删除['+row.title+']吗',(re)=>{
                if (re){
                    this.$ajax({
                        url:this.$url({app:'admin',controller:'driver',action:'del'}),
                        data:{
                            name:row.name,
                            type:this.type,
                            app:this.app
                        },
                        success:(data)=>{
                            this.getList();
                        }
                   });
                }
            })
        },
        add(){

            if (this.param == undefined) return;

            this.$dialog({
                title:'添加驱动',
                type:'vue',
                content:admin.createApp(editDialog,{type:this.type,app:this.app,param:this.param}),
                callback:()=>{
                    this.getList();
                }
            });
        }
    },
    mounted(){
        this.getList();
    },
}
</script>